package com.reefe.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.reefe.config.redis.RedisUtil;
import com.reefe.dto.ContentCatDto;
import com.reefe.dto.DtoUtil;
import com.reefe.entity.ZTreeNode;
import com.reefe.exception.AppException;
import com.reefe.mapper.TbContentCategoryMapper;
import com.reefe.pojo.TbContentCategory;
import com.reefe.pojo.TbContentCategoryExample;
import com.reefe.redis.JedisClient;
import com.reefe.service.ContentCatService;
import com.reefe.utils.FastJsonConvert;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by xiao on 2018-03-07.
 */
@Component
@Service(version = "1.0.0",timeout = 3000)
public class ContentCatServiceImpl implements ContentCatService {
    private final static Logger log= Logger.getLogger(ContentCatServiceImpl.class);
    @Value("${INDEX_CONTENT_CAT}")
    private String INDEX_CONTENT_CAT;
    @Autowired
    private JedisClient jedisClient;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;

    @Override
    public List<ZTreeNode> getContentCatList(Long parentId) {
        //查询缓存
        try{
            //有缓存则读取
            String json = jedisClient.hget(INDEX_CONTENT_CAT,parentId+"");
            if(json!=null){
                List<ZTreeNode> list = FastJsonConvert.convertJSONToObject(json, List.class);
                return list;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        TbContentCategoryExample example=new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria=example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        example.setOrderByClause("sort_order");
        List<TbContentCategory> catList=tbContentCategoryMapper.selectByExample(example);

        List<ZTreeNode> list=new ArrayList<>();

        for(int i=0;i<catList.size();i++){
            ZTreeNode zTreeNode= DtoUtil.TbContentCategory2ZTreeNode(catList.get(i));
            list.add(zTreeNode);
        }
        try {
            jedisClient.hset(INDEX_CONTENT_CAT,parentId+"", FastJsonConvert.convertObjectToJSON(list));
            //设置过期时间
//          jedisClient.expire(RDEIS_ITEM+":"+id,ITEM_EXPIRE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int updateContentCat(ContentCatDto contentCatDto) {
        //同步缓存
        deleteHomeRedis();
        TbContentCategory tbContentCategory = DtoUtil.ContentCatDto2TbContentCategory(contentCatDto);
        TbContentCategory old = getContentCatById(tbContentCategory.getId());
        tbContentCategory.setUpdated(new Date());
        tbContentCategory.setParentId(old.getParentId());
        tbContentCategory.setIcon(old.getIcon());
        tbContentCategory.setCreated(old.getCreated());
        if(tbContentCategoryMapper.updateByPrimaryKey(tbContentCategory)!=1){
            throw new AppException("更新内容分类失败");
        }
        return 1;
    }

    @Override
    public TbContentCategory getContentCatById(Long id) {
        TbContentCategory tbContentCategory = tbContentCategoryMapper.selectByPrimaryKey(id);
        if(tbContentCategory==null){
            throw new AppException("通过id获得内容分类失败");
        }
        return tbContentCategory;
    }

    @Override
    public int addContentCat(ContentCatDto contentCatDto) {
        //同步缓存
        deleteHomeRedis();
        TbContentCategory tbContentCategory = DtoUtil.ContentCatDto2TbContentCategory(contentCatDto);
        tbContentCategory.setParentId((long) 0);
        tbContentCategory.setCreated(new Date());
        tbContentCategory.setUpdated(new Date());
        if(tbContentCategoryMapper.insert(tbContentCategory)!=1){
            throw new AppException("新增内容分类失败");
        }

        return 1;
    }

    @Override
    public int deleteContentCat(Long id) {
        //同步缓存
        deleteHomeRedis();
        if(tbContentCategoryMapper.deleteByPrimaryKey(id)!=1){
            throw new AppException("删除内容分类失败");
        }
        return 1;
    }
    //同步首页缓存
    public void deleteHomeRedis(){
        try {
            jedisClient.hdel(INDEX_CONTENT_CAT,"");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
